const data0 = JSON.parse(localStorage.getItem("工资条数据data"));
const data = data0.data;

console.log(data);
function renderBody(data, currentPage = 1) {
    singlePageNum = 5;
    renderPaging(data, currentPage);
    const statIndex = singlePageNum * (currentPage - 1);
    const endIndex = statIndex + singlePageNum;
    const newData = data.slice(statIndex, endIndex);
    $("#information tbody").empty();
    for (let i = 0; i < newData.length; ++i) {
        $("tbody").append(`<tr> 
        <td> ${newData[i].salarySheet}</td>
        <td>${newData[i].已发送}</td>
        <td>${newData[i].已查看}</td>
        <td>${newData[i].已确认}</td>
        <td>${newData[i].生成日期}</td>
        <td>${newData[i].操作人}</td>
            <td>
            <button type="button" class="btn btn-info particulars">发送工资条</button>
            </td>
        </tr>
            `
        );
    }
}

function renderPaging(data, currentPage = 1) {
    $("#pages").empty();
    const num = Math.ceil(data.length / singlePageNum);
    for (let i = 1; i <= num; ++i) {
        $("#pages").append(`<span>${i}</span>`);
    }
    $("#pages > span").eq(currentPage - 1).addClass("currentPage");
}

renderBody(data);

$("#pages").on("click", "span", function (event) {
    const currentPage = parseInt($(event.target).text());
    renderBody(data, currentPage);
});
// 上一页
$("#pre").on("click", function () {
    const currentPage = parseInt($("#pages > .currentPage").text());
    if (currentPage === 1) {
        return;
    }
    else {
        renderBody(data, currentPage - 1);
    }
});
// 下一页
$("#next").on("click", function () {
    const currentPage = parseInt($("#pages > .currentPage").text());
    if (currentPage === $("#pages span").length) {
        return;
    }
    else {
        renderBody(data, currentPage + 1);
    }
});

$("#reset").on("click", function () {
    $("#screen").empty();
    let matter = $(".condition input").val();
    $("#screen").append(`
    <span class="screen">筛选条件：</span>
    <span class="trem ">${matter}</span>
    `);
    let newData = [];
    for (let i = 0; i < data.length; ++i) {
        for (let key in data[i]) {
            if (matter === data[i][key]) {
                newData.push(data[i]);
            }
        }
    }
    renderBody(newData);
});

$(".particulars").on("click",function(){
    window.location.href ="../薪资/发送工资条.html"
})
